package com.sensoro.www.viewstubtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button showHideStubButton;
    LinearLayout stubLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initCtrl();
    }

    void initCtrl() {
        showHideStubButton = (Button) findViewById(R.id.activity_main_btn);
        showHideStubButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.activity_main_btn:
                showHideStub();
                break;
        }
    }

    void showHideStub() {
        if (stubLayout == null) {
            ViewStub viewStub = (ViewStub) findViewById(R.id.activity_main_viewstub);
            stubLayout = (LinearLayout) viewStub.inflate();
            return;
        }
        if (stubLayout.getVisibility() == View.VISIBLE) {
            stubLayout.setVisibility(View.INVISIBLE);
        } else if (stubLayout.getVisibility() == View.INVISIBLE) {
            stubLayout.setVisibility(View.VISIBLE);
        }
    }
}
